package com.yangsir.mysite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangsir.mysite.Dto.CommentDto;
import com.yangsir.mysite.Dto.CommentNew;
import com.yangsir.mysite.common.R;
import com.yangsir.mysite.mapper.ArticleMapper;
import com.yangsir.mysite.mapper.CommentMapper;
import com.yangsir.mysite.pojo.Article;
import com.yangsir.mysite.pojo.Comment;
import com.yangsir.mysite.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author yf
* @description 针对表【comment(文章的评论)】的数据库操作Service实现
* @createDate 2024-06-22 16:55:16
*/
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 保存评论
     * @param comment
     * @return
     */
    @Override
    public R<String> saveComment(Comment comment) {
        if (comment.getContext() == "") {
            return R.error("评论内容不能为空");
        } else if (comment.getArticleId() == null) {
            return R.error("文章id不能为空");
        }
        if (commentMapper.insert(comment)==1?false:true) {
            return R.error("评论失败");
        }
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", comment.getArticleId());
        int result = articleMapper.update(null, updateWrapper.setSql("comment_num = comment_num + 1"));

        System.out.println(result > 0?"评论数量更新成功":"评论数量更新失败");
        return R.success("评论成功");
    }

    /**
     * 获取评论
     * @param id
     * @return
     */
    @Override
    public R<List<CommentDto>> getComment(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId, id);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentDto> commentDtos = new ArrayList<>();
        comments.forEach(comment -> {
            if (comment.getParentId() == null) {
                CommentDto commentDto = new CommentDto();
                BeanUtils.copyProperties(comment, commentDto);
                commentDtos.add(commentDto);
            }
        });
        for (CommentDto commentDto : commentDtos) {
            List<CommentNew> commentSon = new ArrayList<>();
            for (Comment comment : comments) {
                if (comment.getParentId() == null) {
                    continue;
                }
                if (commentDto.getId().equals(comment.getParentId())) {
                    CommentNew commentNew = new CommentNew();
                    BeanUtils.copyProperties(comment, commentNew);
                    commentSon.add(commentNew);
                }
            }
            commentDto.setCommentSon(commentSon);
        }
        return R.success(commentDtos, "获取评论成功");
    }
}




